package com.neusoft.hifly.information.article.entity;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.htmlparser.Parser;
import org.htmlparser.visitors.TextExtractingVisitor;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.neusoft.hifly.core.utils.GlobalConstants;

/**
 * Title: 文章资讯服务
 * <p>
 * Description: 文章内容对象
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@TableName(value = GlobalConstants.DYNAMIC_TABLE_NAME_PREFIX + "_INFO_ARTICLE_INFO")
public class ArticleInfoObj implements Serializable {
	private static final long serialVersionUID = -159183567806558L;
	// 租户逻辑ID
	@TableField(value = "ORGAN_LOGIC_ID")
	private long organLogicId;
	// 文章逻辑ID
	@TableId(value = "ARTICLE_LOGIC_ID")
	private long articleLogicId;
	// 内容
	@TableField(value = "CONTENT")
	private String content;
	// 存文本内容
	@TableField(value = "CONTENT_TXT")
	private String contentTxt;

	// ################### 临时数据 ####################
	// 租户编码
	@TableField(exist = false)
	private String organCode;

	/**
	 * 得到对象的哈希值
	 *
	 * @return int 对象的哈希值
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getArticleLogicId()).toHashCode();
	}

	/**
	 * 比较两个对象是否相等
	 *
	 * @param other
	 *            要比较的对象
	 * @return boolean 相同返回true，否则返回flase
	 */
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof ArticleInfoObj)) {
			return false;
		}
		final ArticleInfoObj castOther = (ArticleInfoObj) other;
		return new EqualsBuilder().append(this.getArticleLogicId(), castOther.getArticleLogicId()).isEquals();
	}

	/**
	 * 返回对象的字符串值
	 *
	 * @return String 表示用户对象的字符串值
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this).append("文章内容", this.getArticleLogicId()).toString();
	}

	/**
	 * 提取存文本
	 *
	 * @return 存文本
	 */
	public String getContentTxt() {
		if (StringUtils.isEmpty(contentTxt) && StringUtils.isNotEmpty(getContent())) {
			try {
				final Parser parser = new Parser("<div>" + getContent() + "</div>");
				final TextExtractingVisitor visitor = new TextExtractingVisitor();
				parser.visitAllNodesWith(visitor);
				contentTxt = visitor.getExtractedText();
				contentTxt = contentTxt.replace("\r\n", "");
			} catch (final Exception e) {
				contentTxt = getContent();
			}
		}
		return contentTxt;
	}

	public long getOrganLogicId() {
		return organLogicId;
	}

	public void setOrganLogicId(final long organLogicId) {
		this.organLogicId = organLogicId;
	}

	public long getArticleLogicId() {
		return articleLogicId;
	}

	public void setArticleLogicId(final long articleLogicId) {
		this.articleLogicId = articleLogicId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(final String content) {
		this.content = content;
	}

	public String getOrganCode() {
		return organCode;
	}

	public void setOrganCode(final String organCode) {
		this.organCode = organCode;
	}

	public void setContentTxt(final String contentTxt) {
		this.contentTxt = contentTxt;
	}

}